package org.neuedu.service;

import org.neuedu.bean.*;
import org.neuedu.dao.*;

import java.util.List;

public class ArticleServiceImpl implements ArticleService {
    private ArticleDao articleDao = new ArticleDaoImpl();
    private CategoryDao categoryDao=new CategoryDaoImpl();
    private ReplyDao replyDao =new ReplyDaoImpl();
    @Override
    public int articlePublish(Article article) {
       int i = articleDao.saveArticleInfo(article);
       if(i!=0){
           articleDao.updateKissNum(article);
           return 1;
       }
        return 0;
    }

    @Override
    public IndexLoader loadIndexInfo(Integer tid) {
        // 获取文章分类信息
        List<Category> categoryList = categoryDao.serchAllCateListByRole();
        // 获取置顶信息
        List<Article> topArticleList = articleDao.getTopArticleList();
        // 获取主要文章信息
        List<Article> tenMainArticleList = articleDao.getTenMainArticleList(tid);

        // 获取回答周榜
        List<UserReply> replyUserList =articleDao.getReplyUserList();
        // 获取本周热议
        List<redgreadArticle> redgreadArticles = articleDao.getredgreadArticle();
        IndexLoader indexLoader = new IndexLoader();
        indexLoader.setTopArticleList(topArticleList);
        indexLoader.setMainArticleList(tenMainArticleList);
        indexLoader.setCategoryList(categoryList);
        indexLoader.setGetredgreadArticle(redgreadArticles);
        indexLoader.setUserReplyList(replyUserList);
        return indexLoader;
    }


    @Override
    public Detail getArticleDetailById(Integer id) {
        Detail detail = new Detail();
        // 查询文章详情+作者信息
        Article article = articleDao.getArticleById(id);
        // 查询文章评论信息
        List<reply> reply = articleDao.getReplyByAid(id);

        article.setReplyList(reply);

        detail.setArticle(article);



        articleDao.updateArticleViewsById(id);
        return detail;
    }

    @Override
    public IndexLoader loadIndexInfoCid(Integer cid) {




        List<Article> CidMainArticleList = articleDao.CidmainArticleList(cid);
        IndexLoader indexLoader = new IndexLoader();

        indexLoader.setCidmainArticleList(CidMainArticleList);

        return indexLoader;
    }

    @Override
    public IndexLoader getArticleById(Integer uid) {
        // 查询文章评论信息
        IndexLoader indexLoader =new IndexLoader();
        List<Article> articleByUid = articleDao.getArticleByUid(uid);

        indexLoader.setGetArticle(articleByUid);



        return indexLoader;
    }

    @Override
    public Article getArticleDetailByUid(Integer uid) {
        Article article =new Article();

        // 查询文章评论信息
        List<reply> reply = articleDao.getReplyByUid(uid);

        article.setReplyList(reply);


        return article;
}

    @Override
    public IndexLoader getarticleByCid(Integer uid) {
        IndexLoader indexLoader =new IndexLoader();
        List<Article> articleByCid = articleDao.getArticleByCid(uid);
        List<reply> replyByUid = replyDao.getReplyByUid(uid);
        indexLoader.setArticleListByCid(articleByCid);
        indexLoader.setReplyListByUid(replyByUid);
        return indexLoader;

    }


}
